package com.example.shop_2302.login

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Cons
import com.example.shop_2302.R
import com.example.shop_2302.databinding.ActivityLoginCodeBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
@Route(path = "/app/LoginCodeActivity")
class LoginCodeActivity : BaseActivity<ActivityLoginCodeBinding,LoginViewModel>() {
    override fun initView() {
        binding.btnSendcode.setOnClickListener {
            val phone = binding.etPhone.text.toString()
            if(phone.isEmpty()){
                ToastUtils.showLong("手机号不能为空")
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileSimple(phone)){
                ToastUtils.showLong("手机号格式不正确")
                return@setOnClickListener
            }
            viewModel.sendCode(phone)
            //发送验证码后按钮不可用  然后开启倒计时，倒计时结束后可用
            binding.btnSendcode.isEnabled = false
            lifecycleScope.launch {
                for (i in 15 downTo 1){
                    binding.btnSendcode.text = "$i 秒后获取"
                    delay(1000)
                    if(i==1){
                        binding.btnSendcode.text = "获取验证码"
                        binding.btnSendcode.isEnabled = true
                    }
                }
            }
        }
        binding.btnLogin.setOnClickListener {
            val phone = binding.etPhone.text.toString()
            val code = binding.etCode.text.toString()
            if(phone.isEmpty()||code.isEmpty()){
                ToastUtils.showLong("手机号和验证码均不能为空")
                return@setOnClickListener
            }
            if(phone.isEmpty()){
                ToastUtils.showLong("手机号不能为空")
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileSimple(phone)){
                ToastUtils.showLong("手机号格式不正确")
                return@setOnClickListener
            }
            if(!binding.cbFwb.isChecked){
                ToastUtils.showLong("请先勾选同意协议")
                return@setOnClickListener
            }
            viewModel.loginCode(code,phone)
        }
        //富文本
        val ssb = SpannableStringBuilder(binding.cbFwb.text.toString())
        //文字颜色
        ssb.setSpan(ForegroundColorSpan(Color.RED),2,4,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        //文字点击
        val clickableSpan = object:ClickableSpan(){
            override fun onClick(widget: View) {
                ARouter.getInstance().build("/app/WebViewActivity").withString("url","http://www.baidu.com").navigation()
            }
        }
        val clickableSpan2 = object:ClickableSpan(){
            override fun onClick(widget: View) {
                ARouter.getInstance().build("/app/WebViewActivity").withString("url","http://www.sina.com").navigation()
            }
        }
        ssb.setSpan(clickableSpan,7,13,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        ssb.setSpan(clickableSpan2,14,20,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        binding.cbFwb.movementMethod = LinkMovementMethod.getInstance()//防止点击事件不起作用
        binding.cbFwb.text = ssb
    }

    override fun initData() {
        viewModel.sendSuccess.observe(this){
            if(it.code==200){
                //binding.etCode.setText(it.data.content.substring(8,14))
                //发送通知 //通知管理器 //通知对象  //发送通知

                val manager =
                    getSystemService(NOTIFICATION_SERVICE) as NotificationManager
                //高版本  通知通道
                if(Build.VERSION.SDK_INT>Build.VERSION_CODES.O){
                    val notification = Notification.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("验证码")
                        .setContentText("验证码为${it.data.content.substring(8,14)}")
                        .setChannelId("channelId")
                        .build()
                    val notificationChannel = NotificationChannel(
                        "channelId",
                        "channelName",
                        NotificationManager.IMPORTANCE_DEFAULT
                    )
                    manager.createNotificationChannel(notificationChannel)
                    manager.notify(1,notification)

                }else{
                    //低版本
                    val notification = Notification.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("验证码")
                        .setContentText("验证码为${it.data.content.substring(8,14)}")
                        .build()
                    manager.notify(1,notification)
                }


            }
        }
        viewModel.codeSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                //进行跳转  保存用户信息
                SPUtils.getInstance().put(Cons.SP_TOKEN,it.data.token)
                SPUtils.getInstance().put(Cons.SP_PHONE,it.data.phone)
                finish()
                ARouter.getInstance().build( "/app/ShopActivity").navigation()//跳转到主页
            }
        }
        viewModel.codeFail.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getViewModel(): Class<LoginViewModel>  = LoginViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_login_code

}